package com.desay.pangoo.itmt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.desay.pangoo.common.lite.lang.web.PangooWebConstant;
import com.desay.pangoo.common.lite.spring.web.ControllerCommonUtils;
import com.desay.pangoo.itmt.entity.ModifyLogEntity;
import com.desay.pangoo.itmt.repository.ModifyLogEntityRepository;
import com.desay.pangoo.itmt.repository.spec.ExpandCriteria;
import com.desay.pangoo.itmt.repository.spec.Restrictions;

/**
 * @author uidq1163
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ModifyLogServiceImpl {

	@Autowired
	ModifyLogEntityRepository modifyLogEntityRepository;

	/**
	 * 保存数据变更日志
	 * 
	 * @param modifyLogEntity
	 */
	public void saveLog(ModifyLogEntity modifyLogEntity) {
		String user = ControllerCommonUtils.getHeader(PangooWebConstant.HEADER_NAME_USER_FULL_NAME);
		modifyLogEntity.setCreator(user);
		modifyLogEntityRepository.save(modifyLogEntity);
	}

	/**
	 * 获取日志信息
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param associatedId
	 * @param tableName
	 * @return
	 */
	public Page<ModifyLogEntity> getList(Integer pageSize, Integer pageNum, Long associatedId, String tableName,
			String techPtn) {
		ExpandCriteria<ModifyLogEntity> c = new ExpandCriteria<>();
		// 根据组查询
		c.add(Restrictions.eq("associatedId", associatedId, true));
		c.add(Restrictions.eq("associatedTable", tableName, true));
		c.add(Restrictions.like("techPtn", techPtn, true));
		List<Order> orders = new ArrayList<>();
		Order order = new Order(Sort.Direction.DESC, "createTime");
		orders.add(order);
		Pageable pageReq = PageRequest.of(pageNum - 1, pageSize, Sort.by(orders));
		return modifyLogEntityRepository.findAll(c, pageReq);
	}

}
